﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EditAnyString.Providers;
using EditAnyString.Web.DataTable;
using EditAnyString.Models;

namespace EditAnyString.Web.Areas.admin.Controllers
{
	/// <summary>
	/// This is simple controller to handle data editing and viewing.
	/// </summary>
    public class EASController : Controller
    {
        private readonly IEASStorageProvider _easStorageProvider;

		public EASController( IEASStorageProvider easStorageProvider )
		{
			_easStorageProvider = easStorageProvider;
		}

		public ActionResult LoadEASToStorage()
		{
			ViewBag.Added = _easStorageProvider.StoreEASData( T4EAS.EAS.Strings );
			ViewBag.EASStorageProviderName = _easStorageProvider.Name;
			return View();
		}

		public ActionResult ViewEASData()
		{
			return View();
		}

		[HttpPost]
		public ActionResult ListEASData( [DataTableModelBinder] DataTableModel dataTable )
		{
			int total = 0;
			int? filterByKey = dataTable.GetSearchValue<int?>( 0, null );
			string filterByContext = dataTable.GetSearchValue<string>( 1, null );
			string filterByValue = dataTable.GetSearchValue<string>( 2, null );

			var list = _easStorageProvider.Search( filterByKey, filterByContext, filterByValue,
				dataTable.iDisplayStart / dataTable.iDisplayLength, dataTable.iDisplayLength, out total );

			return DataTable( dataTable, total, total, list, e => new string[] 
			{ 
				e.Key.ToString(), 
				e.ContextWithIndex,
				e.Value,
				string.Format("<a href='#' class='editlink' key='{0}' context='{1}'>edit</a>", e.Key, e.Context)
			}, e => e.Key.ToString() );
		}

		[HttpPost]
		public ActionResult GetStringInfo( int key )
		{
			EditableString found = _easStorageProvider.GetByKey(key );
			if( found == null )
				return Json( new { success = false }, JsonRequestBehavior.AllowGet );
			return Json( new
			{
				success = true,
				key = found.Key,
				context = found.Context,
				value = found.Value
			} );
		}

		[HttpPost]
		[ValidateInput( false )]
		public ActionResult SaveStringInfo( int key, string value )
		{
			bool ok = _easStorageProvider.Update( key, value );
			if( ok )
				return Json( new { success = true, newValue = value } );
			return Json( new { success = false } );
		}

		protected ActionResult DataTable<ElementType>( DataTableModel dataTable, int iTotalRecords, int iTotalDisplayRecords, IList<ElementType> list,
			Func<ElementType, string[]> row, Func<ElementType, string> rowId )
		{
			var res = new DataTableResult( dataTable, iTotalRecords, iTotalDisplayRecords );
			for( int i = 0; i < list.Count; ++i )
			{
				string[] cols = row( list[ i ] );
				Dictionary<string, string> resCols = new Dictionary<string, string>();
				for( int c = 0; c < cols.Length; ++c )
				{
					resCols.Add( c.ToString(), cols[ c ] );
				}
				// check if we want row id
				if( rowId != null )
					resCols.Add( "DT_RowId", "dtrow_" + rowId( list[ i ] ) );
				res.aaData.Add( resCols );
			}
			return res;
		}

    }
}
